namespace RushMail.Requests;

/// <summary>
/// 跟踪统计（SMTP 对接）
/// </summary>
public class Track
{
    private readonly IRushMailClient _client;

    public Track(IRushMailClient client)
    {
        _client = client;
    }

    /// <summary>
    /// 跟踪统计概况
    /// http://edmapi.rushmail.com/api.php?module=track&action=smtp-overview
    /// </summary>
    /// <returns></returns>
    public async Task<RushMailResponse<ReportTaskListResponse>> SmtpOverview(DateOnly date) =>
        (await _client.PostAsync(new RushMailRequest("track", "smtp-overview")
        {
            Body = new Dictionary<string, string>(){ {"date", date.ToString("yyyy-mm-dd")} }
        })).ToGeneric<ReportTaskListResponse>();
    
    /// <summary>
    /// 跟踪统计详情
    /// http://edmapi.rushmail.com/api.php?module=track&action=smtp-detail
    /// </summary>
    /// <returns></returns>
    public async Task<RushMailResponse<ReportTaskListResponse>> SmtpDetail(int trackId) =>
        (await _client.PostAsync(new RushMailRequest("track", "smtp-detail")
        {
            Body = new Dictionary<string, string>(){ {"track_id", trackId.ToString()} }
        })).ToGeneric<ReportTaskListResponse>();
    
    /// <summary>
    /// 跟踪统计链接列表
    /// http://edmapi.rushmail.com/api.php?module=track&action=smtp-link-list
    /// </summary>
    /// <returns></returns>
    public async Task<RushMailResponse<ReportTaskListResponse>> SmtpLinkList(int trackId) =>
        (await _client.PostAsync(new RushMailRequest("track", "smtp-link-list")
        {
            Body = new Dictionary<string, string>(){ {"track_id", trackId.ToString()} }
        })).ToGeneric<ReportTaskListResponse>();


    /// <summary>
    /// 跟踪统计链接详细
    /// http://edmapi.rushmail.com/api.php?module=track&action=smtp-link-detail
    /// </summary>
    /// <returns></returns>
    public async Task<RushMailResponse<int>> SmtpLinkDetail(int trackId , int linkId) =>
        (await _client.PostAsync(new RushMailRequest("track", "smtp-link-detail")
        {
            Body = new Dictionary<string, string>(){ {"track_id", trackId.ToString()}, {"link_id", linkId.ToString()} }
        }))
        .ToGeneric<int>();
    
    /// <summary>
    /// 跟踪统计概况
    /// http://edmapi.rushmail.com/api.php?module=track&action=overview
    /// </summary>
    /// <returns></returns>
    public async Task<RushMailResponse<TrackOverviewResponse>> Overview(int taskId) =>
        (await _client.PostAsync(new RushMailRequest("track", "overview")
        {
            Body = new Dictionary<string, string>(){ {"task_id", taskId.ToString()} }
        })).ToGeneric<TrackOverviewResponse>();
    
    /// <summary>
    /// 跟踪统计详情
    /// http://edmapi.rushmail.com/api.php?module=track&action=detail
    /// </summary>
    /// <returns></returns>
    public async Task<RushMailResponse<List<TrackDetailItem>>> Detail(int trackId) =>
        (await _client.PostAsync(new RushMailRequest("track", "detail")
        {
            Body = new Dictionary<string, string>(){ {"track_id", trackId.ToString()} }
        })).ToGeneric<List<TrackDetailItem>>();
    
    /// <summary>
    /// 跟踪统计链接列表
    /// http://edmapi.rushmail.com/api.php?module=track&action=smtp-link-list
    /// </summary>
    /// <returns></returns>
    public async Task<RushMailResponse<ReportTaskListResponse>> LinkList(int trackId) =>
        (await _client.PostAsync(new RushMailRequest("track", "link-list")
        {
            Body = new Dictionary<string, string>(){ {"track_id", trackId.ToString()} }
        })).ToGeneric<ReportTaskListResponse>();


    /// <summary>
    /// 跟踪统计链接详细
    /// http://edmapi.rushmail.com/api.php?module=track&action=link-detail
    /// </summary>
    /// <returns></returns>
    public async Task<RushMailResponse<int>> LinkDetail(int trackId , int linkId) =>
        (await _client.PostAsync(new RushMailRequest("track", "link-detail")
        {
            Body = new Dictionary<string, string>(){ {"track_id", trackId.ToString()}, {"link_id", linkId.ToString()} }
        }))
        .ToGeneric<int>();
}


public class TrackOverviewResponse
{
    [JsonProperty("track_id")]public int TrackId { get; set; }
    [JsonProperty("open_unique")]public int OpenUnique { get; set; }
    [JsonProperty("open_total")]public int OpenTotal { get; set; }
    [JsonProperty("failure_total")]public int FailureTotal { get; set; }
}

public class TrackDetailItem
{
    public string Address { get; set; }
    [JsonProperty("open_count")]public int OpenCount { get; set; }
    [JsonProperty("click_count")]public int ClickCount { get; set; }
    [JsonProperty("ipaddress")]public string IpAddress { get; set; }
    public string Region { get; set; }
}

